import {messages} from "share/common";
/**
 * 操作：更改
 * 适用：已通过 且 customFormPropertyMap['application.change.enable']为true 的 差旅申请单
 * 获取 customFormPropertyMap 的接口：/api/custom/forms/
 */
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'
import { Form, Button, message, Modal } from 'antd'

import requestService from 'containers/request/request.service'

class TravelUpdateBtn extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
    }
  }

  //判断是否可以更改
  judgeEnable = () => {
    this.setState({ loading: true });
    requestService.judgeEnableChange(this.props.info.applicationOID).then(res => {
      if (res && res.data && res.data.message === 'AUTO_FLIGHT') {
        //是否有智能机票行程
        //弹框提示是否继续提交
        Modal.confirm({
          title: messages('request.key173'/*提示*/),
          content: <div>
            <p>{messages('request.key535')/*更改单据会先取消授权智能订票任务，是否确定？*/}</p>
          </div>,
          okText: messages('common.ok')/*确认*/,
          cancelText: messages('request.key175'/*取消*/),
          onOk: ()=> this.handleUpload(),
          onCancel: () => {this.setState({ loading: false })},
        });
      } else {
        this.handleUpload();
      }
    }).catch(err => {
      if (err.response && err.response.data && err.response.data.message) {
        this.setState({ loading: false });
        message.warning(err.response.data.message);
      }
    });
  };

  //更改
  handleUpload = () => {
    const { formOID, applicationOID } = this.props.info;
    const { info } = this.props;
    let applicationInfo = JSON.parse(JSON.stringify(info));
    applicationInfo.applicationOID = '';
    requestService.handleApplicationUpload(applicationOID, applicationInfo).then(res => {
      // 差旅申请单属于「出行及预订」
      this.context.router.replace(menuRoute.getRouteItem('travel-and-booking-edit').url.replace(':formOID', formOID).replace(':applicationOID', res.data.applicationOID))
    }).catch(e => {
      this.setState({ loading: false });
      const errMsg = e.response.data.message || '';
      const errCode = e.response.data.errorCode;
      if (errCode === '11005' || errCode === '11004') {
        const errArr = errMsg.split('<br/>') || [];
        //弹框提示
        Modal.info({
          title: messages('request.key173'/*提示*/),
          content: <div>
            {errArr.map(e => (<p>{e}</p>))}
          </div>,
          okText: messages('common.ok')/*确认*/,
          width: 1000
        });
      } else {
        message.warning(errMsg);
      }
    })
  };

  render() {
    const { loading } = this.state;
    const { formType, info, updateEnable } = this.props;
    return (
      <div className="travel-update-btn request-btn">
        {/* status: 1003（已通过）、1011（已更改） */}
        {formType === 2001 && info.status === 1003 && updateEnable === 'true' && (
          <Button type="primary" loading={loading} onClick={this.judgeEnable}>{messages('request.key99'/*更 改*/)}</Button>
        )}
      </div>
    )
  }
}

TravelUpdateBtn.propTypes = {
  formType: React.PropTypes.number.isRequired,
  info: React.PropTypes.object.isRequired,
  updateEnable: React.PropTypes.string
};

TravelUpdateBtn.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps() {
  return { }
}

const wrappedTravelUpdateBtn = Form.create()(TravelUpdateBtn);

export default connect(mapStateToProps)(wrappedTravelUpdateBtn)
